/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AddPost.java
 *
 * Created on 10/04/2011, 01:37:22
 */

package GUI;

import Business.FrameHandler;
import Domain.DataManager;
import Domain.UserClient.ButtonsFunctions;
import Logger.ClientLogger;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.util.Vector;
import java.util.logging.Level;
import objects.Trd;

/**
 *
 * @author Rubik Levitsky
 */
public class AddPost extends javax.swing.JFrame {

    private String _trd;

    /** Creates new form AddPost */
    public AddPost(String thread) {
        initComponents();

        Toolkit tk = Toolkit.getDefaultToolkit();
        Dimension screenSize = tk.getScreenSize();
        int screenHeight = screenSize.height;
        int screenWidth = screenSize.width;
        setLocation(screenWidth / 4, screenHeight / 4);

        this._trd = thread;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        chooseFourmLabel = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        chooseFourmLabel1 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Add Post");

        chooseFourmLabel.setFont(new java.awt.Font("Tahoma", 0, 18));
        chooseFourmLabel.setForeground(new java.awt.Color(0, 102, 102));
        chooseFourmLabel.setText("Enter Message content:");

        jButton1.setLabel("Ok");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        chooseFourmLabel1.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        chooseFourmLabel1.setForeground(new java.awt.Color(0, 102, 102));
        chooseFourmLabel1.setText("Enter Message title:");

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane1.setViewportView(jTextArea1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(jTextField2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 314, Short.MAX_VALUE)
                        .addComponent(chooseFourmLabel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 199, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(chooseFourmLabel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 199, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 314, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(25, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(chooseFourmLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(chooseFourmLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton1)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
          String content = jTextArea1.getText();
          String title = jTextField2.getText();
        if(jTextArea1.getText().isEmpty() | jTextArea1.getText().isEmpty())
        {
            String error = "You have to enter some text in your post AND title";
            FrameHandler.getFrameHandler().showErrorMessage(error);
        }
        else if(this._trd == null)
        {
            String error = "You have to choose a thread before posting a message";
            FrameHandler.getFrameHandler().showErrorMessage(error);
        }
        else{
            ButtonsFunctions.getButtonsFunctions().processAddMessage(this._trd, title, content);
            if(DataManager.getDataManager().getSuccessOrFailure() == DataManager.FAILURE)
            {
                    ClientLogger.write(Level.INFO,"received failure");
                    return;
            }
            String forum = DataManager.getDataManager().getCurrForum();
            ButtonsFunctions.getButtonsFunctions().processGetThreads(forum);
            Vector<Trd> threads = DataManager.getDataManager().getVectorThreads();
            FrameHandler.getFrameHandler().changeCurrentFrame(new Forum(threads));
            this.dispose();
        }
}//GEN-LAST:event_jButton1ActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel chooseFourmLabel;
    private javax.swing.JLabel chooseFourmLabel1;
    private javax.swing.JButton jButton1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextField jTextField2;
    // End of variables declaration//GEN-END:variables

}
